#include "Game Engine\Header\KinematicBody.h"
#include "Game Engine\Header\Camera.h"

KinematicBody::KinematicBody()
{
	mMass		= 10.0f;
	mBody		= NULL;
}

KinematicBody::~KinematicBody()
{
}

void KinematicBody::Init( ID3D10Device* device, btDynamicsWorld* world, std::wstring meshFile,
	ID3D10Effect* fx, ID3D10ShaderResourceView* cubeMap, float mass,
	Physics::Shape shape, bool useShadows )
{
	Body::Init( device, world, meshFile, fx, cubeMap, mass, shape, useShadows );

	mGhostObject = CreateGhostObject( mDynamicsWorld, mMesh, mMass, shape );
	btScalar stepHeight = btScalar( 0.35 );

	mCharacter = new btKinematicCharacterController( mGhostObject, (btConvexShape*)mGhostObject->getCollisionShape(), stepHeight, mass );
}

void KinematicBody::Init( ID3D10Device* device, btDynamicsWorld* world, std::vector<std::wstring> meshFiles,
	ID3D10Effect* fx, ID3D10ShaderResourceView* cubeMap, float mass,
	Physics::Shape shape, bool useShadows )
{
	Body::Init( device, world, meshFiles, fx, cubeMap, mass, shape, useShadows );

	btTransform trans;

	trans.setIdentity();
	trans.setOrigin( BTVector3( mPosition ) );
	mGhostObject = CreateGhostObject( mDynamicsWorld, mMass, trans, mCompound );
	btTransform loadTrans;
	loadTrans.setIdentity();
	loadTrans.setOrigin( BTVector3( mPosition ) );
	mGhostObject->activate();
	mGhostObject->setWorldTransform( loadTrans );
	mGhostObject->setVelocity( btVector3( 0.0f, 0.0f, 0.0f ) );
	btScalar stepHeight = btScalar( 0.35 );

	mCharacter = new btKinematicCharacterController( mGhostObject, (btConvexShape*)mCompound, stepHeight, mass );
}

D3DXVECTOR3& KinematicBody::GetPosition()
{
	mPosition = BT2DX_VECTOR3( mCharacter->getPosition() );

	return mPosition;
}

D3DXVECTOR3& KinematicBody::GetVelocity()
{
	//mVelocity = BT2DX_VECTOR3( mGhostObject->getVelocity() );

	return mVelocity;
}

void KinematicBody::SetPosition( D3DXVECTOR3 pos )
{
	mPosition = pos;

	mCharacter->warp( BTVector3( mPosition ) );
}

void KinematicBody::SetVelocity( D3DXVECTOR3 vel )
{
	mVelocity = vel;

	mGhostObject->setVelocity( BTVector3( mVelocity ) );
}

void KinematicBody::Strafe( float d )
{
	D3DXVECTOR3 r = GetCamera().GetCameraRight();
	//mVelocity.x += d;
	mVelocity += d * GetCamera().GetCameraRight();
}

void KinematicBody::Walk( float d )
{
	//mVelocity.z += d;
	mVelocity += d * GetCamera().GetCameraLook();
	//mVelocity.x += d * GetCamera().GetCameraLook().x;
	//mVelocity.z += d * GetCamera().GetCameraLook().z;
}

void KinematicBody::Move( float dt )
{
	mVelocity.y *= 0.8f;
	mVelocity.x *= 0.8f;
	mVelocity.z *= 0.8f;

	//mVelocity += BT2DX_VECTOR3( mGhostObject->getVelocity() );

	mCharacter->setWalkDirection( BTVector3( mVelocity ) );
}

void KinematicBody::Stop()
{
	mVelocity = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
	mCharacter->setWalkDirection( btVector3( 0.0f, 0.0f, 0.0f ) );
}

void KinematicBody::Draw( D3DXMATRIX& lightViewProj )
{
	if( mGhostObject->getCollisionShape()->isCompound() )
	{
		btCompoundShape* compound = ( btCompoundShape* )mGhostObject->getCollisionShape();
		for( int i = 0; i < compound->getNumChildShapes(); i++ )
		{
			btTransform child = compound->getChildTransform(i);
			btTransform ms = mGhostObject->getWorldTransform() * child;
			mMeshVector[i]->Draw( BT2DX_MATRIX( ms ), lightViewProj );
		}
	}
	else
	{
		btTransform bodyTrans;
		bodyTrans.setIdentity();
		bodyTrans.setOrigin( btVector3( mGhostObject->getWorldTransform().getOrigin().x(),
			mGhostObject->getWorldTransform().getOrigin().y(), mGhostObject->getWorldTransform().getOrigin().z() ) );
		btTransform trans = mGhostObject->getWorldTransform();
		mWorld = BT2DX_MATRIX( trans );

		mMesh->Draw( mWorld, lightViewProj );
	}
}